<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Identitas_Sekolah extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_identitas';

		$this->primary_key = 'aka_identitas.identitas_id';

	}
	function get_identitas_by_departemen($id){
		$this->load->database();
		
		if($id == ""){
			$sql = "SELECT aka_identitas.*,aka_departemen.nama as name  FROM aka_identitas LEFT JOIN aka_departemen ON aka_identitas.departemen_id=aka_departemen.departemen_id WHERE aka_departemen.departemen_id is NULL";
			
		} else {
			$sql = "SELECT aka_identitas.*,aka_departemen.nama as name  FROM aka_identitas LEFT JOIN aka_departemen ON aka_identitas.departemen_id=aka_departemen.departemen_id WHERE aka_departemen.departemen_id='$id' ";
		}
							
		$query= $this->db->query($sql);
		
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$data[] = $row;
			}
		}
		$query->free_result();
					
		$this->db->close();
		return $query;
	}
	
	function get_identitas_by_id($id){
		if($id == "")
			$id = 2;
		$this->load->database();
		
			 $sql = "SELECT aka_identitas.*,aka_departemen.nama as name  FROM aka_identitas LEFT JOIN aka_departemen ON aka_identitas.departemen_id=aka_departemen.departemen_id WHERE aka_identitas.identitas_id='$id'";

			$query = $this->db->query($sql);
		
		$this->db->close();
		
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert('aka_identitas',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where('identitas_id',$id);
				$this->db->update('aka_identitas',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM aka_identitas WHERE identitas_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>